package io.github.definitlyevil.ziminmod.listeners;

import io.github.definitlyevil.customitemframework.CIFItemInstance;
import io.github.definitlyevil.customitemframework.CustomItemFramework;
import io.github.definitlyevil.customitemframework.ItemRegister;
import io.github.definitlyevil.ziminmod.cif.armors.ZiMinGreenHat;
import io.github.definitlyevil.ziminmod.cif.armors.ZiMinHelmet;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.Random;

public class ZiMinHelmetListener implements Listener {

    private final Random random = new Random();

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onProjectileHit(ProjectileHitEvent event) {
        if(!Arrow.class.isAssignableFrom(event.getEntity().getClass())) return;
        if(random.nextFloat() > 0.5) return;
        if(event.getHitEntity() == null || !checkHelmet(event.getHitEntity())) return;
        Entity old = event.getEntity();
        World world = old.getWorld();
        Location location = old.getLocation();
        Vector vel = old.getVelocity().multiply(-1d);
        location.add(vel.clone().normalize().multiply(1.2f));
        float speed = (float) vel.length();

        old.remove();

        world.spawnArrow(location, vel, speed, 0.02f);
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onEntityDamage(EntityDamageByEntityEvent event) {
        if(!checkHelmet(event.getEntity())) return;
        if(!LivingEntity.class.isAssignableFrom(event.getDamager().getClass())) return;
        if(random.nextFloat() > 0.5f) return;
        // LivingEntity target = (LivingEntity) event.getEntity();
        LivingEntity damager = (LivingEntity) event.getDamager();
        Location location = damager.getLocation();
        if(damager.getEquipment() == null) return;

        ItemStack oldHelmet = damager.getEquipment().getHelmet();
        if(oldHelmet != null && oldHelmet.getType() != Material.AIR) {
            // check if it's green hat
            CIFItemInstance green = CustomItemFramework.getCIFDataFromItem(oldHelmet);
            if(green == null || !ZiMinGreenHat.class.isAssignableFrom(green.type.getClass())) {
                // drop that shit!
                location.getWorld().dropItemNaturally(location, oldHelmet);
            }
        }
        damager.getEquipment().setHelmet(null);

        ItemStack green = ItemRegister.get("ZiMinGreenHat").generateItemStack(null, false);
        damager.getEquipment().setHelmet(green);

        damager.sendMessage("\u00a7a\u00a7l你受到了针男人帽子的作用，被戴上了绿帽子！");
    }

    /**
     * 检查穿的是不是针男人帽子
     * @param entity
     * @return
     */
    private static boolean checkHelmet(Entity entity){
        if(entity == null || !LivingEntity.class.isAssignableFrom(entity.getClass())) return false;
        LivingEntity living = (LivingEntity) entity;
        if(living.getEquipment() == null || living.getEquipment().getHelmet() == null) return false;
        // 获取这个实体的头盔
        ItemStack itemStack = living.getEquipment().getHelmet();
        CIFItemInstance instance = CustomItemFramework.getCIFDataFromItem(itemStack);
        return instance != null && ZiMinHelmet.class.isAssignableFrom(instance.type.getClass());
    }

}
